<template>
  <div>
    <a-modal v-model:visible="visible" title="推送类别设置" width="700px" @cancel="handleonCancel">
      <div class="flex headerBorder">
        <div>
          一级类别
          <a href="javascript:;" @click="handleOneAdd" style="margin-right: 10px">
            <img src="@/assets/add.png" alt="" />
          </a>
        </div>
        <div style="padding-left: 10px">
          二级类别
          <a href="javascript:;" @click="handleTwoAdd"><img src="@/assets/add.png" alt="" /></a>
        </div>
      </div>
      <div class="flex classification" style="margin-top: 13px">
        <div class="oneborder">
          <div
            v-for="(item, index) in typeList"
            :key="index"
            class="primary"
            :style="{ background: `${activeIndex == index ? '#1F1F1F' : ''}` }"
          >
            <span style="width: 80%">
              <a-input
                v-model:value="item.name"
                v-if="index == inputIndex || !item.name"
                style="height: 30px; background: #1e202a"
                :style="{ width: `${activeIndex == index ? '110%' : '100%'}` }"
                @click="handleEdit(index)"
                @blur="handleBlur"
                ref="inputRef"
              />
              <span v-else>{{ item.name }}</span>
            </span>
            <span style="float: right">
              <a
                @click="handleEdit(index)"
                v-if="activeIndex == index ? false : true"
                class="classDel"
              >
                <img src="@/assets/edit.png" alt="" />
              </a>
              <span v-show="item.editFlag == 0 ? false : true">
                <a @click="handleOneDel(index, item)">
                  <img src="@/assets/del.png" alt="" />
                </a>
              </span>
            </span>
          </div>
        </div>
        <div style="width: 100%">
          <div v-for="(item, index) in secondList" :key="index" class="primary">
            <span style="width: 90%">
              <a-input
                v-model:value="item.name"
                v-if="index == secondIndex || !item.name"
                style="height: 30px"
                :style="{ width: `${index == secondIndex ? '102%' : '95%'}` }"
                ref="inputtwoRef"
                @click="handleTwoEdit(index)"
              />
              <span v-else class="textinstance">{{ item.name }}</span>
            </span>
            <span style="float: right; display: flex">
              <a
                @click="handleTwoEdit(index)"
                style="margin-right: 10px"
                v-if="index == secondIndex ? false : true"
              >
                <img src="@/assets/edit.png" alt="" />
              </a>
              <span v-show="item.editFlag == 0 ? false : true">
                <a @click="handleTwoDel(index, item)">
                  <img src="@/assets/del.png" alt="" />
                </a>
              </span>
            </span>
          </div>
        </div>
      </div>
      <template v-slot:footer>
        <div class="footer">
          <a-button @click="handleonCancel">取消</a-button>
          <a-button type="primary" @click="handleOk" :loading="loading">保存设置</a-button>
        </div>
      </template>
    </a-modal>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, nextTick, onActivated, createVNode } from 'vue';
import { ProcessTypeTree, ProcessTypeUpdate, ProcessTypeDelete } from '@/api/smart-tools/index';
import { message, Modal } from 'ant-design-vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
export default defineComponent({
  setup(props, context) {
    const loading = ref<boolean>(false);
    const typeList = ref<any>([]);
    const secondList = ref<any>([]);
    const secondIndex = ref<number>(-1);
    const inputIndex = ref<number>(-1);
    const index = ref<number>(-1);
    const visible = ref<boolean>(false);

    const showModal = () => {
      visible.value = true;
    };
    //一级编辑
    const inputRef = ref();
    const handleEdit = value => {
      inputIndex.value = value;
      index.value = value;
      activeIndex.value = value;
      secondList.value = typeList.value[value].children;
      nextTick(() => {
        // inputRef.value.focus();
      });
      secondIndex.value = -1;
    };
    //一级新增
    const activeIndex = ref<number>(-1);
    const handleOneAdd = () => {
      typeList.value.push({
        name: '',
        children: [],
      });
      activeIndex.value = typeList.value.length - 1;
      index.value = typeList.value.length - 1;
      inputIndex.value = typeList.value.length - 1;
      secondList.value = [];
    };
    //一级删除
    const handleOneDel = (index, item) => {
      if (item.id) {
        Modal.confirm({
          title: '您确定要删除该一级类别吗?同时也会将该级别下的二级类别同时删除!',
          icon: createVNode(ExclamationCircleOutlined),
          okText: '是',
          okType: 'danger',
          cancelText: '否',
          onOk() {
            ProcessTypeDelete({ enableFlag: 0, id: item.id }).then(res => {
              if (res.code == 200) {
                typeList.value.splice(index, 1);
                secondList.value = [];
                message.success('删除成功!');
              }
            });
          },
        });
      } else {
        typeList.value.splice(index, 1);
      }
    };
    //二级编辑
    const inputtwoRef = ref();
    const handleTwoEdit = value => {
      secondIndex.value = value;
      nextTick(() => {
        // inputtwoRef.value.focus();
      });
    };
    //二级新增
    const handleTwoAdd = () => {
      if (typeList.value[index.value].children == null) {
        typeList.value[index.value].children = [];
      }
      typeList.value[index.value].children.push({});
      secondList.value = typeList.value[index.value].children;
      secondIndex.value = secondList.value.length - 1;
    };
    //二级删除
    const handleTwoDel = (indexel, item) => {
      if (item.id) {
        Modal.confirm({
          title: '您确定要删除吗?',
          icon: createVNode(ExclamationCircleOutlined),
          okText: '是',
          okType: 'danger',
          cancelText: '否',
          onOk() {
            ProcessTypeDelete({ enableFlag: 0, id: item.id }).then(res => {
              if (res.code == 200) {
                typeList.value[index.value].children.splice(indexel, 1);
                message.success('删除成功!');
              }
            });
          },
        });
      } else {
        typeList.value[index.value].children.splice(indexel, 1);
      }
    };
    //失去焦点
    const handleBlur = () => {
      inputIndex.value = -1;
    };
    //保存
    const handleOk = () => {
      let obj = JSON.stringify(typeList.value);
      loading.value = true;
      let flag = true;
      typeList.value.forEach(item => {
        if (!item.name) {
          flag = false;
          loading.value = false;
          message.error('一级存在未填写的类别,请核实后再保存!');
          throw Error();
        }
        if (item.children?.length) {
          item.children.forEach(el => {
            if (!el.name) {
              flag = false;
              loading.value = false;
              message.error('二级存在未填写的类别,请核实后再保存!');
              throw Error();
            }
          });
        }
      });
      if (flag) {
        ProcessTypeUpdate({ list: obj })
          .then(res => {
            if (res.code == 200) {
              message.success('成功!');
              visible.value = false;
              loading.value = false;
              context.emit('handleQuery');
            }
          })
          .finally(() => {
            loading.value = false;
          });
      }
    };
    const handleProcessTypeTree = () => {
      //初始化
      secondIndex.value = -1;
      inputIndex.value = 0;
      index.value = 0;
      activeIndex.value = 0;
      ProcessTypeTree().then(res => {
        typeList.value = res.data;
        secondList.value = typeList.value[0].children;
      });
    };
    const handleonCancel = () => {
      visible.value = false;
      context.emit('handleQuery');
    };
    onActivated(() => {
      //   handleProcessTypeTree();
    });
    return {
      handleonCancel,
      loading,
      activeIndex,
      handleProcessTypeTree,
      inputtwoRef,
      handleOneDel,
      handleTwoDel,
      inputRef,
      handleBlur,
      handleEdit,
      handleTwoAdd,
      handleTwoEdit,
      handleOneAdd,
      secondIndex,
      inputIndex,
      index,
      visible,
      showModal,
      handleOk,
      typeList,
      secondList,
    };
  },
});
</script>
<style lang="less" scoped>
.flex {
  display: flex;
}
.headerBorder {
  height: 30px;
  padding: 0 10px;
  background: #3b3d44;
  border: 1px solid #555e77;
}
.primary {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100% !important;
  height: 45px;
  padding: 10px;
  border-bottom: 1px solid #555e77;
  .textinstance {
    display: block;
    width: 90%;
    height: 28px;
    padding-left: 10px;
    background: #3b3d44;
    border: 1px solid #51596f;
  }
  .classDel {
    position: absolute;
    top: 10px;
    right: 30px;
  }
}

.classification {
  border: 1px solid #555e77;
}
.oneborder {
  background: #3b3d44;
  border-right: 1px solid #555e77;
}
.classification div:nth-child(1) {
  width: 68%;
}
.headerBorder div:nth-child(1) {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 40%;
  border-right: 1px solid #555e77;
}
.headerBorder div:nth-child(2) {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 60%;
}
.footer {
  display: flex;
  justify-content: center;
  width: 100%;
}
:deep(.ant-modal-header) {
  background-color: red;
}
</style>
